const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
  data: {
    puid:0,
    uid:0,
    agreeTrue:false,
  },
  onShow: function (){
    if(wx.getStorageSync('uid')){
      this.getPhone()
    }
  },
  getPhone:function(){
    var data = {
      'uid':wx.getStorageSync('uid')
    }
    $api.post('member/phone',data).then(res =>{
      if(res.data.code == 1){
        wx.switchTab({
          url: '../index/index'
        })
      }else{
        //有缓存  没有验证手机
        wx.navigateTo({
          url:'/pages/index/phone'
        })
        return;
      }
    })
  },
  bindGetUserInfo: function(e){
    var that = this
    this.setData({
      agreeTrue:false
    })
    if(this.data.userTrue){
      var userinfo = e.detail.userInfo;
      if(userinfo){
        wx.setStorageSync('nickName',userinfo.nickName);
        wx.setStorageSync('avatarUrl',userinfo.avatarUrl);
        var info = {
          'nickName':userinfo.nickName,
          'avatarUrl':userinfo.avatarUrl
        }
        that.wxLogin(info)
      }else{
        //拒绝授权
        this.setData({
          agreeTrue:true
        })
      }
    }else{
      wx.showToast({
        title:'请同意用户协议!!',
        duration:3000,
        mask:true, 
        icon:'none',
      })
      return;
    }
  },
  //微信登陆
  wxLogin:function(info){
    var that = this
    wx.login({
      success (res){
        var data = {'code':res.code}
        //that.getData(info);
        $api.get('getopenid',data).then(res =>{
            info.openid = res.data.openid
            info.session_key = res.data.session_key
            that.getData(info);
        })
      }
    })
  },
  getData:function(info){
    var that = this
    wx.getUserInfo({
      success: function(res){
        info.iv = res.iv,
        info.encryptedData = res.encryptedData
        // $api.post('member/unionid',info).then(res =>{
        //   info.unionId = res.data.data.unionId
        //   that.addUser(info)
        //   //console.log(res);
        //   //return;
        // })
        that.addUser(info)
      }
    })
    //console.log(info);
  },
  //添加用户e
  addUser:function(info){
    var data ={
        'openId':info.openid,
        'nickName':info.nickName,
        'avatarUrl':info.avatarUrl,
        'puid':wx.getStorageSync('sharePuid'),
        'uid':wx.getStorageSync('shareUid'),
        'session_key':info.session_key,
        'iv':info.iv,
        'encryptedData':info.encryptedData
    }
    var that = this
    $api.post('user/login',data).then(res => {
      if(res.data.code == 1){
        wx.removeStorageSync('sharePuid')
        wx.removeStorageSync('shareUid')
        wx.setStorageSync('uid',res.data.data.id);
        wx.setStorageSync('role',res.data.data.role);
        //手机号码不存在  弹出手机验证码页面
        if(res.data.data.phone){
          wx.switchTab({
            url: '../index/index'
          })
        }else{
          wx.navigateTo({
            url:'/pages/index/phone'
          })
        }
      }
    })
  },
  bindAcceptRule:function(e){
    if (e.detail.value.length == 0) {
      var userTrue = false
      var agreeTrue = false
    }else{
      var userTrue = true
      var agreeTrue = true
    }
    this.setData({
      userTrue:userTrue,
      agreeTrue:agreeTrue
    })
  },
  back:function(){
    wx.switchTab({
      url: '../index/index'
    })
  }
})
